package com.woniuxy.opencv.controller;

import com.woniuxy.opencv.pojo.ResData;
import com.woniuxy.opencv.service.FaceRecognitionService;
import com.woniuxy.opencv.service.impl.LockService;
import com.woniuxy.opencv.client.LightFeign;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/face")
@CrossOrigin
@Slf4j
public class FaceUnlockController {

    @Autowired
    private LockService lockService;
    @Autowired
    private FaceRecognitionService faceRecognitionService;
    @Autowired
    private LightFeign lightFeign;

    @PostMapping("/unlock")
    public ResData faceUnlock(@RequestBody Map<String, String> request) {
        String faceImage = request.get("faceImageUrl");
        Integer lockId = Integer.parseInt(request.get("lockId"));
        // 1. 人脸验证
        boolean verified = faceRecognitionService.verifyFace(faceImage);
        if (!verified) {
            return new ResData(401, "人脸验证失败", null);
        }
        // 2. 开锁
        ResData resData = lockService.handleLockOperation(lockId);
        if (resData.getCode() == 200) {
            try {
                lightFeign.updateUsedStatus(lockId.longValue(), 1);
            } catch (Exception e) {

                log.error(""+e);

            }
        }
        return resData;
    }
}
